package com.example.a14874.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Button btnstartservice;
    private Button btnstopservice;
    private Button btnbangservice;
    private Button btnunbangservice;
    private ServiceConnection serviceConnection;
    private MyBinder myBinder;
    private  Button btnAdd;
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        btnstartservice=findViewById( R.id.btn_start_service );
        btnstopservice=findViewById( R.id.btn_stop_service );
        btnbangservice=findViewById( R.id.btn_bang_service );
        btnunbangservice=findViewById( R.id.btn_unbang_service );
        btnAdd=findViewById( R.id.btn_add );

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected( ComponentName name, IBinder iBinder ) {
                Log.e( "MainActivity","onServiceConnected" );
                myBinder=(MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected( ComponentName name ) {
                Log.e( "MainActivity","onServiceDisconnected" );
            }

            @Override
            public void onBindingDied( ComponentName name ) {
                Log.e( "MainActivity","onBindingDied" );
            }
        };

        btnbangservice.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Intent intent=new Intent(  );
                intent.setClass( MainActivity.this,MyService.class );
                bindService( intent,serviceConnection,BIND_AUTO_CREATE );
            }
        } );

        btnunbangservice.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                unbindService( serviceConnection );
            }
        } );
        btnAdd.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                String time=myBinder.getTime();
                Toast.makeText( MainActivity.this, time, Toast.LENGTH_SHORT ).show();
            }
        } );

        btnstartservice.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Intent intent = new Intent(  );
                intent.setClass( MainActivity.this,MyService.class );
                startService( intent );
            }
        } );

        btnstopservice.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Intent intent = new Intent(  );
                intent.setClass( MainActivity.this,MyService.class );
                stopService( intent );
            }
        } );
    }
}
